var express = require('express')
var app = express()
var router = require('./router.js')
var path = require('path')
var bodyParser = require('body-parser')

//设置允许跨域访问该服务.
app.all('*', function(req, res, next) {
    //设置允许跨域的域名，*代表允许任意域名跨域
    res.header("Access-Control-Allow-Origin", req.headers.origin);
    //允许的header类型
    res.header("Access-Control-Allow-Headers", "content-type, Authorization");
    //跨域允许的请求方式 
    res.header("Access-Control-Allow-Methods", "DELETE,PUT,POST,GET,OPTIONS");
    res.header('Content-Type', 'application/json;charset=utf-8');
    res.header('Access-Control-Allow-Credentials', 'false');
    next();
});

app.use(express.static(path.join(__dirname, './dist/')))
app.use('/public/', express.static(path.join(__dirname, './public/')))
app.use('/node_modules/', express.static(path.join(__dirname, './node_modules/')))

app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())

app.engine('html', require("express-art-template"))
app.set('views', path.join(__dirname, './views/')) //设置render函数的文件寻找路径



app.use(router)


app.use(function(req, res) {
    res.render('404.thml')
})
//配置处理404
app.use(function(error, req, res, next) { //全局中间件
    return res.status(500).json({
        err_code: 500,
        msg: "Internal server err"
    })
})

app.listen(3000, function() {
    console.log("running at http://127.0.0.1:3000...")
})